import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/user/wallet/xindong_exchange_yemian.dart';
import 'package:xindong/routes/user/income/xindong_income_list_yemian.dart';
import 'package:xindong/routes/user/wallet/xindong_wallet_account_yemian.dart';
import 'package:xindong/routes/user/wallet/xindong_wallet_ctr.dart';
import 'package:xindong/routes/user/wallet/xindong_withdraw_yemian.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_app_bar_text_button.dart';
import 'package:xindong/widgets/common/xindong_button.dart';

class XinDongWalletYeMian extends StatefulWidget {
  const XinDongWalletYeMian({super.key});

  @override
  State<XinDongWalletYeMian> createState() => _XinDongWalletYeMianState();
}

class _XinDongWalletYeMianState extends State<XinDongWalletYeMian> {
  @override
  void initState() {
    super.initState();
    XinDongUserController.to.requestUserScore();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar(
        "钱包",
        actions: [
          XinDongAppBarTextButton(
            name: "清单",
            onTap: () {
              XinDongPageSingleton.instance.pushYeMian(const XinDongIncomeListYeMian());
            },
          )
        ],
      ),
      body: GetBuilder<XinDongUserController>(builder: (controller) {
        return GetBuilder<XinDongWalletCtr>(initState: (s) {
          XinDongWalletCtr.to.requestGetRate();
        }, builder: (wallet) {
          return ListView(
            children: [
              _buildItem(
                title: "积分余额",
                value: "${wallet.score}",
                iconName: AssetsRes.MONEY_JIFEN,
                onTap: () => XinDongPageSingleton.instance.pushYeMian(const XinDongExchangeYeMian()),
              ),
              _buildItem(
                title: "冻结余额",
                value: "${wallet.dongJieScore}",
                iconName: AssetsRes.MONEY_JIFEN,
                onTap: () => XinDongPageSingleton.instance.pushYeMian(const XinDongExchangeYeMian()),
              ),
              _buildItem(title: "最多可提现", value: wallet.withdrawText, iconName: AssetsRes.MONEY_KETIXIAN, unit: ""),
              _buildButton(),
              TextButton(
                onPressed: () => XinDongPageSingleton.instance.pushYeMian(const XinDongWalletAccountYeMian()),
                child: const Text(
                  "修改支付宝",
                  style: TextStyle(color: DZColors.mainColor),
                ),
              ),
              TextButton(
                onPressed: () => XinDongPageSingleton.instance.jumpToWebYeMian(
                  title: "提现说明",
                  url: XinDongNetworking.instance.withdrawUrl,
                ),
                child: const Text(
                  "提现说明",
                  style: TextStyle(color: DZColors.mainColor),
                ),
              ),
            ],
          );
        });
      }),
    );
  }

  Widget _buildItem(
      {required String title, required String value, required String iconName, String unit = "积分", Function()? onTap}) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: kCellHeight,
        color: DZColors.cellColor,
        padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
        margin: const EdgeInsets.only(bottom: 1),
        child: Row(
          children: [
            ImageUtil.asset(iconName, width: 30, height: 30),
            const SizedBox(width: 10),
            Text(
              title,
              style: const TextStyle(color: DZColors.titleColor, fontSize: 16, height: 1.2),
            ),
            const Expanded(child: SizedBox()),
            Text(
              "$value$unit",
              style: const TextStyle(color: DZColors.moneyColor, fontSize: 15, height: 1.2),
            ),
            if (onTap != null) XinDongAll.instance.defaultIconNext else const SizedBox(width: 10),
          ],
        ),
      ),
    );
  }

  Widget _buildButton() {
    return Container(
      height: kButtonHeight,
      margin: const EdgeInsets.fromLTRB(26, 30, 26, 20),
      child: Row(
        children: [
          Expanded(
            flex: 1,
            child: XinDongCommonBorderButton(
              "提现",
              onTap: () => XinDongPageSingleton.instance.pushYeMian(const XinDongWithDrawYeMian()),
            ),
          ),
          // const SizedBox(width: 20),
          // Expanded(
          //   flex: 1,
          //   child: XinDongCommonButton(
          //     "积分商城",
          //     onTap: () {},
          //   ),
          // )
        ],
      ),
    );
  }
}
